<template>
  <div class="chooseAddress-content">
    <web-view @message="handleMsg" :src="webViewSrc"></web-view>
  </div>
</template>
<script>
import { setToken } from "@/utils/auth";
import { updatePubUserPhone } from "@/service/api/sp-common/user";
import { mapState } from "vuex";

export default {
  props: {
    webViewUrl: String,
  },
  data() {
    return {};
  },
  computed: {
    webViewSrc() {
      return decodeURIComponent(this.webViewUrl);
    },
    ...mapState("sp-common/auth", ["updatePubUserParams"]),
  },
  methods: {
    handleMsg(e) {
      console.log("监听的回调", e);
      if (e.detail.data[0].token.token) {
        setToken(e.detail.data[0].token.token);
        this.$store.commit(
          "sp-common/auth/SET_TOKEN",
          e.detail.data[0].token.token
        );
        this.$nextTick(() => {
          this.$store.commit("sp-common/auth/SET_ANON_USER", false);
          this.$store.dispatch("sp-common/auth/userLogin", false).then(() => {
            this.handleUpdatePubUserPhone();
          });
        });
      }
    },
    async handleUpdatePubUserPhone() {
      const res = await updatePubUserPhone({
        ...this.updatePubUserParams,
      });
      if (res.code === 0) {
        await this.$store.dispatch("sp-common/user/getUserBaseInfoAction");
      }
    },
  },
};
</script>
<style lang="scss" scoped></style>
